import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
  AdminUserEditState state,
  Dispatch dispatch,
  ViewService viewService,
) {
  return Scaffold(
    appBar: AppBar(
      title: Text(state.mode == 'add'
          ? '新建管理员'
          : state.mode == 'edit' ? '编辑管理员' : '管理员信息'),
      actions: <Widget>[
        if (state.mode == 'detail')
          IconButton(
            icon: Icon(Icons.edit),
            onPressed: () {
              dispatch(AdminUserEditActionCreator.onEdit());
            },
          )
      ],
    ),
    body: buildForm(state, dispatch, viewService),
  );
}

Widget buildForm(
  AdminUserEditState state,
  Dispatch dispatch,
  ViewService viewService,
) {
  return SingleChildScrollView(
    padding: EdgeInsets.all(10),
    child: Column(
      children: <Widget>[
        FormBuilder(
          key: state.formKey,
          child: Column(
            children: <Widget>[
              FormBuilderTextField(
                attribute: 'nickName',
                initialValue: state.rawModel.nickName,
                readonly: state.mode == 'detail',
                decoration: InputDecoration(
                  labelText: '姓名',
                  helperText: '请输入姓名',
                  icon: Icon(Icons.person),
                ),
                validators: [
                  FormBuilderValidators.required(errorText: '姓名不能为空'),
                ],
                onChanged: (val) {
                  state.formModel.nickName = val;
                },
              ),
              if (state.mode == 'add')
                FormBuilderTextField(
                  attribute: 'userName',
                  initialValue: state.rawModel.userName,
                  readonly: state.mode == 'detail',
                  decoration: InputDecoration(
                    labelText: '用户名',
                    helperText: '请输入用户名',
                    icon: Icon(Icons.person),
                  ),
                  validators: [
                    FormBuilderValidators.required(errorText: '用户名不能为空'),
                  ],
                  onChanged: (val) {
                    state.formModel.userName = val;
                  },
                ),
              if (state.mode != 'edit')
                FormBuilderTextField(
                  readonly: true,
                  attribute: 'password',
                  initialValue: state.rawModel.password,
                  obscureText: true,
                  decoration: InputDecoration(
                    labelText: '密码',
                    helperText: '请输入密码',
                    icon: Icon(Icons.lock),
                  ),
                  validators: [
                    FormBuilderValidators.required(errorText: '密码不能为空'),
                  ],
                  onChanged: (val) {
                    state.formModel.password = val;
                  },
                ),
            ],
          ),
        ),
        if (state.mode != 'detail')
          Row(
            children: <Widget>[
              Expanded(
                child: RaisedButton(
                  color: Theme.of(viewService.context).primaryColor,
                  child: Text(
                    '保存',
                    style: TextStyle(color: Colors.white),
                  ),
                  onPressed: () {
                    dispatch(AdminUserEditActionCreator.onSave());
                  },
                ),
              ),
              SizedBox(
                width: 20,
              ),
              Expanded(
                child: RaisedButton(
                  color: Theme.of(viewService.context).primaryColor,
                  child: Text(
                    '重置',
                    style: TextStyle(color: Colors.white),
                  ),
                  onPressed: () {
                    dispatch(AdminUserEditActionCreator.onReset());
                  },
                ),
              ),
            ],
          ),
      ],
    ),
  );
}
